from django.shortcuts import render
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from datetime import datetime
import hashlib
from common.models import Goods,Types,Orders,Detail,Users

# 商品导航信息加载
def loadPNav(request):
    '''商品导航信息加载'''
    context={}
    plists=Types.objects.filter(pid=0)
    print(plists)
    context["typelist"]=plists
    return context

def viporders(request):
    '''用户订单'''
    context=loadPNav(request)
    # 获取当前用户所有订单
    orderlist=Orders.objects.filter(uid=request.session["vipuser"]["id"]).order_by("state","-addtime")
    # 获取订单详情
    for order in orderlist:
        detaillist=Detail.objects.filter(orderid=order.id)
        for d in detaillist:
            d.picname=Goods.objects.only("picname").get(id=d.goodsid).picname
        order.detaillist=detaillist
    context["orderslist"]=orderlist
    return render(request,"web/viporders.html",context)

def orderstate(request):
    '''修改订单状态'''
    try:
        # 获取订单id
        orderID=request.GET.get("oid","0")
        orderOBJ=Orders.objects.get(id=orderID)
        orderOBJ.state=request.GET.get("state")
        orderOBJ.save()
        return redirect(reverse("vip_orders"))
    except Exception as err:
        print("修改订单状态错误：",err)
        context={"info":"订单处理失败！"}
        return render(request,'web/info.html',context)

# -----会员信息-----
def info(request):
    '''个人信息'''
    try:
        # 从缓存获取会员id
        uid = request.session["vipuser"]["id"]
        print(uid)
        # 根据id获取会员信息
        vipinfo = Users.objects.get(id=uid)
        print(vipinfo)
        context={'user':vipinfo}
        return render(request, 'web/vipinfo.html',context)
    except Exception as err:
        print("查询信息失败",err)
        context={"info":"查询信息失败！"}
    return render(request,'web/info.html',context)

def update(request):
    '''执行个人信息编辑'''
    try:
        # 从缓存获取会员id
        uid = request.session["vipuser"]["id"]
        uobj = Users.objects.get(id=uid)
        uobj.name = request.POST["name"]
        uobj.sex = request.POST["sex"]
        uobj.address = request.POST["address"]
        uobj.code = request.POST["code"]
        uobj.phone = request.POST["phone"]
        uobj.email = request.POST["email"]
        uobj.save()
        context = {"info": "修改成功！"}
    except Exception as err:
        context = {"info": "修改失败！"}
    return render(request, 'web/info.html', context)

# 重置密码
def pwd(request):
    '''加载重置密码页面'''
    try:
        uid = request.session["vipuser"]["id"]
        uobj=Users.objects.get(id=uid)
        context={'user':uobj}
        return render(request, 'web/pwd.html',context)
    except Exception as err:
        context={"info":"密码重置失败！"}
    return render(request,'web/info.html',context)
def pwdSave(request):
    '''执行密码重置'''
    try:
        uid = request.session["vipuser"]["id"]
        uobj=Users.objects.get(id=uid)
        pwd = request.POST["password"] #密码
        repwd=request.POST["repassword"]#重复密码
        if pwd!=repwd:# 密码不一致
            context = {"info": "两次输入密码不一致！"}
        else:
            m=hashlib.md5()
            m.update(bytes(pwd,encoding='utf8'))
            uobj.password = m.hexdigest()
            uobj.save()
            context = {"info": "密码重置成功！"}
    except Exception as err:
        print("密码重置失败",err)
        context={"info":"密码重置失败！"}
    return render(request,'web/info.html',context)
